import { plugin, Messagetype } from 'alemon'
import db from '../../db/sqlite/config.js'
import { QueryTypes } from 'sequelize'
export class K_gjcList extends plugin {
  constructor() {
    super({
      dsc: '关键词列表',
      rule: [
        {
          reg: '^/关键词列表',
          fnc: '方法名'
        }
      ]
    })
  }
  async 方法名(e: Messagetype) {
    const eventType = e.eventId.split(':')[0]
    // 只允许私域消息
    if (eventType !== 'MESSAGE_CREATE') {
      return
    }
    const data = await db
      .query('select * from k_gjc where guild = ?', {
        replacements: [e.msg.guild_id],
        type: QueryTypes.SELECT
      })
      .catch(err => err.parent)

    if ('errno' in data) {
      return e.reply('查询数据库失败')
    }

    if (data[0]) {
      const msg: string[] = []
      for (let i = 0; i < data.length; i++) {
        msg.push(i + 1 + '、' + data[i].key + '\n')
      }
      return e.reply(msg)
    } else {
      return e.reply(`<@!${e.msg.author.id}> 暂时没有关键词!`)
    }
  }
}
